package com.castle.fortress.admin.study.exam.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.member.dto.MemberDto;
import com.castle.fortress.admin.study.exam.entity.StudyExamStatisticsEntity;
import com.castle.fortress.admin.study.exam.dto.StudyExamStatisticsDto;
import com.castle.fortress.admin.study.exam.service.StudyExamStatisticsService;
import com.castle.fortress.admin.utils.WebUtil;
import com.castle.fortress.common.utils.ConvertUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.enums.OperationTypeEnum;
import com.castle.fortress.admin.core.annotation.CastleLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;
import org.apache.logging.log4j.util.Strings;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.castle.fortress.admin.utils.ExcelUtils;

import javax.servlet.http.HttpServletResponse;

import com.castle.fortress.common.entity.DynamicExcelEntity;

import java.util.List;

/**
 * 试卷统计 控制器
 *
 * @author castle
 * @since 2023-02-16
 */
@Api(tags = "试卷统计管理控制器")
@Controller
public class StudyExamStatisticsController {
    @Autowired
    private StudyExamStatisticsService studyExamStatisticsService;

    /**
     * 试卷统计的分页展示
     *
     * @param studyExamStatisticsDto 试卷统计实体类
     * @param current                当前页
     * @param size                   每页记录数
     * @return
     */
    @CastleLog(operLocation = "试卷统计", operType = OperationTypeEnum.QUERY)
    @ApiOperation("试卷统计分页展示")
    @GetMapping("/study/exam/studyExamStatistics/page")
    @ResponseBody
    @RequiresPermissions("study:exam:studyExamStatistics:pageList")
    public RespBody<IPage<StudyExamStatisticsDto>> pageStudyExamStatistics(StudyExamStatisticsDto studyExamStatisticsDto, @RequestParam(required = false) Integer current, @RequestParam(required = false) Integer size) {
        Integer pageIndex = current == null ? GlobalConstants.DEFAULT_PAGE_INDEX : current;
        Integer pageSize = size == null ? GlobalConstants.DEFAULT_PAGE_SIZE : size;
        Page<StudyExamStatisticsDto> page = new Page(pageIndex, pageSize);
        IPage<StudyExamStatisticsDto> pages = studyExamStatisticsService.pageStudyExamStatisticsExtends(page, studyExamStatisticsDto);

        return RespBody.data(pages);
    }

    /**
     * 试卷统计的列表展示
     *
     * @param studyExamStatisticsDto 试卷统计实体类
     * @return
     */
    @CastleLog(operLocation = "试卷统计", operType = OperationTypeEnum.QUERY)
    @ApiOperation("试卷统计列表展示")
    @GetMapping("/study/exam/studyExamStatistics/list")
    @ResponseBody
    public RespBody<List<StudyExamStatisticsDto>> listStudyExamStatistics(StudyExamStatisticsDto studyExamStatisticsDto) {
        List<StudyExamStatisticsDto> list = studyExamStatisticsService.listStudyExamStatistics(studyExamStatisticsDto);
        return RespBody.data(list);
    }

    /**
     * 试卷统计保存
     *
     * @param studyExamStatisticsDto 试卷统计实体类
     * @return
     */
    @CastleLog(operLocation = "试卷统计", operType = OperationTypeEnum.INSERT)
    @ApiOperation("试卷统计保存")
    @PostMapping("/study/exam/studyExamStatistics/save")
    @ResponseBody
    @RequiresPermissions("study:exam:studyExamStatistics:save")
    public RespBody<String> saveStudyExamStatistics(@RequestBody StudyExamStatisticsDto studyExamStatisticsDto) {
        if (studyExamStatisticsDto == null) {
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        StudyExamStatisticsEntity studyExamStatisticsEntity = ConvertUtil.transformObj(studyExamStatisticsDto, StudyExamStatisticsEntity.class);
        if (studyExamStatisticsService.save(studyExamStatisticsEntity)) {
            return RespBody.data("保存成功");
        } else {
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 试卷统计编辑
     *
     * @param studyExamStatisticsDto 试卷统计实体类
     * @return
     */
    @CastleLog(operLocation = "试卷统计", operType = OperationTypeEnum.UPDATE)
    @ApiOperation("试卷统计编辑")
    @PostMapping("/study/exam/studyExamStatistics/edit")
    @ResponseBody
    @RequiresPermissions("study:exam:studyExamStatistics:edit")
    public RespBody<String> updateStudyExamStatistics(@RequestBody StudyExamStatisticsDto studyExamStatisticsDto) {
        if (studyExamStatisticsDto == null || studyExamStatisticsDto.getId() == null || studyExamStatisticsDto.getId().equals(0L)) {
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        StudyExamStatisticsEntity studyExamStatisticsEntity = ConvertUtil.transformObj(studyExamStatisticsDto, StudyExamStatisticsEntity.class);
        if (studyExamStatisticsService.updateById(studyExamStatisticsEntity)) {
            return RespBody.data("保存成功");
        } else {
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }


    /**
     * 试卷统计假删除
     *
     * @param id
     * @return
     */
    @CastleLog(operLocation = "试卷统计", operType = OperationTypeEnum.DELETE)
    @ApiOperation("试卷统计假删除")
    @PostMapping("/study/exam/studyExamStatistics/isDelete")
    @ResponseBody
    public RespBody<String> isDeleteStudyExamStatistics(@RequestParam Long id) {
        MemberDto member = WebUtil.currentMember();
        if (member == null) {
            throw new BizException(GlobalRespCode.NO_LOGIN_ERROR);
        }
        if (id == null) {
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        StudyExamStatisticsEntity statisticsEntity = new StudyExamStatisticsEntity();
        statisticsEntity.setExamId(id);
        statisticsEntity.setMemberId(member.getId());
        statisticsEntity.setIsDelete(1);
        if (studyExamStatisticsService.updateByIsDelete(statisticsEntity)) {
            return RespBody.data("保存成功");
        } else {
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 试卷统计删除
     *
     * @param id
     * @return
     */
    @CastleLog(operLocation = "试卷统计", operType = OperationTypeEnum.DELETE)
    @ApiOperation("试卷统计删除")
    @PostMapping("/study/exam/studyExamStatistics/delete")
    @ResponseBody
    @RequiresPermissions("study:exam:studyExamStatistics:delete")
    public RespBody<String> deleteStudyExamStatistics(@RequestParam Long id) {
        if (id == null) {
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if (studyExamStatisticsService.removeById(id)) {
            return RespBody.data("保存成功");
        } else {
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }


    /**
     * 试卷统计批量删除
     *
     * @param ids
     * @return
     */
    @CastleLog(operLocation = "试卷统计", operType = OperationTypeEnum.DELETE)
    @ApiOperation("试卷统计批量删除")
    @PostMapping("/study/exam/studyExamStatistics/deleteBatch")
    @ResponseBody
    @RequiresPermissions("study:exam:studyExamStatistics:deleteBatch")
    public RespBody<String> deleteStudyExamStatisticsBatch(@RequestBody List<Long> ids) {
        if (ids == null || ids.size() < 1) {
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if (studyExamStatisticsService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        } else {
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 试卷统计详情
     *
     * @param id 试卷统计id
     * @return
     */
    @CastleLog(operLocation = "试卷统计", operType = OperationTypeEnum.QUERY)
    @ApiOperation("试卷统计详情")
    @GetMapping("/study/exam/studyExamStatistics/info")
    @ResponseBody
    @RequiresPermissions("study:exam:studyExamStatistics:info")
    public RespBody<StudyExamStatisticsDto> infoStudyExamStatistics(@RequestParam Long id) {
        if (id == null) {
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        StudyExamStatisticsEntity studyExamStatisticsEntity = studyExamStatisticsService.getById(id);
        StudyExamStatisticsDto studyExamStatisticsDto = ConvertUtil.transformObj(studyExamStatisticsEntity, StudyExamStatisticsDto.class);

        return RespBody.data(studyExamStatisticsDto);
    }

    /**
     * 动态表头导出 依据展示的字段导出对应报表
     *
     * @param dynamicExcelEntity
     * @param response
     * @throws Exception
     */
    @CastleLog(operLocation = "试卷统计", operType = OperationTypeEnum.EXPORT)
    @PostMapping("/study/exam/studyExamStatistics/exportDynamic")
    @ApiOperation("动态表头导出，依据展示的字段导出对应报表")
    public void exportDynamic(@RequestBody DynamicExcelEntity<StudyExamStatisticsDto> dynamicExcelEntity, HttpServletResponse response) throws Exception {
        List<StudyExamStatisticsDto> list = studyExamStatisticsService.listStudyExamStatistics(dynamicExcelEntity.getDto());
        //字典、枚举、接口、json常量等转义后的列表数据 根据实际情况初始化该对象，null为list数据直接导出
        List<List<Object>> dataList = null;
        /**
         * 根据实际情况初始化dataList,可参照 com.castle.fortress.admin.system.controller.TmpDemoController类中方法：exportDynamic
         */
        ExcelUtils.exportDynamic(response, dynamicExcelEntity.getFileName() + ".xlsx", null, list, dynamicExcelEntity.getHeaderList(), dataList);
    }


}
